package site.xuxingcan.ninecut.Util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.Log;

/**
 * Created by Administrator on 2016/3/28.
 */
public class BitmapUtil {
    public static Bitmap getRoundBitmap(Bitmap bitmap) {
        //圆形图片宽高
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        //正方形的边长
        int r = 0;
        //取最短边做边长
        if (width > height) {
            r = height;
        } else {
            r = width;
        }
        //构建一个bitmap
        Bitmap backgroundBmp = Bitmap.createBitmap(r, r, Bitmap.Config.ARGB_8888);
        //new一个Canvas，在backgroundBmp上画图
        Canvas canvas = new Canvas(backgroundBmp);
        Paint paint = new Paint();
        //设置边缘光滑，去掉锯齿
        paint.setAntiAlias(true);
        //宽高相等，即正方形
        RectF rect = new RectF(0, 0, r, r);
        //通过制定的rect画一个圆角矩形，当圆角X轴方向的半径等于Y轴方向的半径时，
        //且都等于r/2时，画出来的圆角矩形就是圆形
        canvas.drawRoundRect(rect, r / 2, r / 2, paint);
        //设置当两个图形相交时的模式，SRC_IN为取SRC图形相交的部分，多余的将被去掉
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //canvas将bitmap画在backgroundBmp上
        canvas.drawBitmap(bitmap, null, rect, paint);
        //返回已经绘画好的backgroundBmp
        return backgroundBmp;
    }

    /**
     * 用尽可能低的内存消耗裁剪获取指定大小的bitmap。
     * <p/>
     * 不保持原始图片的宽高比，因此确保返回bitmap的尺寸与设定的相同。
     * <p/>
     */
    public static Bitmap corpBitmapWithGivenParameter(String file, Bitmap.Config inPreferredConfig, int reqWidth, int reqHeight) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file, opts);
        int bitmapWidth, bitmapHeight;
        for (int i = 1; ; i++) {
            bitmapHeight = opts.outHeight / i;
            bitmapWidth = opts.outWidth / i;
            if (bitmapHeight < reqHeight || bitmapWidth < reqWidth) {
                opts.inSampleSize = i - 1;
                break;
            }
        }
        opts.inJustDecodeBounds = false;
        opts.inPreferredConfig = inPreferredConfig;
        Bitmap tempBitmap = BitmapFactory.decodeFile(file, opts);
        if (tempBitmap == null) {
            Log.d("MainActivity", file);
            return null;
        }
        //宽高比
        float originalAspectRatio = (float) tempBitmap.getWidth() / (float) tempBitmap.getHeight();
        float reqAspectRatio = (float) reqWidth / (float) reqHeight;
        Bitmap resizeBmp = tempBitmap;
        if (originalAspectRatio < reqAspectRatio) {
            //高度裁剪
            Matrix matrix = new Matrix();
            int height2get = (int) ((float) tempBitmap.getWidth() / reqAspectRatio);
            int height2corp = tempBitmap.getHeight() - height2get;
            float scaleWidth = (float) reqWidth / (float) tempBitmap.getWidth();
            float scaleHeight = (float) reqHeight / (float) height2get;
            matrix.postScale(scaleWidth, scaleHeight);
            resizeBmp = Bitmap.createBitmap(tempBitmap, 0, height2corp / 2, tempBitmap.getWidth(), height2get, matrix, true);
        } else if (originalAspectRatio >= reqAspectRatio) {
            //宽度裁剪
            Matrix matrix = new Matrix();
            int width2get = (int) (tempBitmap.getHeight() * reqAspectRatio);
            int corpWidth = tempBitmap.getWidth() - width2get;
            float scaleWidth = (float) reqWidth / (float) width2get;
            float scaleHeight = (float) reqHeight / (float) tempBitmap.getHeight();
            matrix.postScale(scaleWidth, scaleHeight);
            resizeBmp = Bitmap.createBitmap(tempBitmap, corpWidth / 2, 0, width2get, tempBitmap.getHeight(), matrix, true);
        }
        if (!tempBitmap.equals(resizeBmp)) {
            tempBitmap.recycle();
        }
        return resizeBmp;
    }

    public static Bitmap corpBitmapWithGivenParameter(Bitmap tempBitmap, int reqWidth, int reqHeight) {
        //宽高比
        float originalAspectRatio = (float) tempBitmap.getWidth() / (float) tempBitmap.getHeight();
        float reqAspectRatio = (float) reqWidth / (float) reqHeight;
        Bitmap resizeBmp = tempBitmap;
        if (originalAspectRatio < reqAspectRatio) {
            //高度裁剪
            Matrix matrix = new Matrix();
            int height2get = (int) ((float) tempBitmap.getWidth() / reqAspectRatio);
            int height2corp = tempBitmap.getHeight() - height2get;
            float scaleWidth = (float) reqWidth / (float) tempBitmap.getWidth();
            float scaleHeight = (float) reqHeight / (float) height2get;
            matrix.postScale(scaleWidth, scaleHeight);
            resizeBmp = Bitmap.createBitmap(tempBitmap, 0, height2corp / 2, tempBitmap.getWidth(), height2get, matrix, true);
        } else if (originalAspectRatio >= reqAspectRatio) {
            //宽度裁剪
            Matrix matrix = new Matrix();
            int width2get = (int) (tempBitmap.getHeight() * reqAspectRatio);
            int corpWidth = tempBitmap.getWidth() - width2get;
            float scaleWidth = (float) reqWidth / (float) width2get;
            float scaleHeight = (float) reqHeight / (float) tempBitmap.getHeight();
            matrix.postScale(scaleWidth, scaleHeight);
            resizeBmp = Bitmap.createBitmap(tempBitmap, corpWidth / 2, 0, width2get, tempBitmap.getHeight(), matrix, true);
        }
        return resizeBmp;
    }

    /**
     * 用尽可能低的内存消耗获取指定大小的bitmap。适用于大图片的裁剪,也可用于小图片的放大
     * <p/>
     * 为了保持原始图片的宽高比，因此不确保返回bitmap的尺寸与设定的相同，仅保证不会超过设置的值。
     *
     * @param file
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap getBitmapWithLowMemory(String file, int reqWidth, int reqHeight) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file, opts);
        int bitmapWidth, bitmapHeight;
        for (int i = 1; ; i++) {
            bitmapHeight = opts.outHeight / i;
            bitmapWidth = opts.outWidth / i;
            if (bitmapHeight < reqHeight || bitmapWidth < reqWidth) {
                opts.inSampleSize = i - 1;
                break;
            }
        }
        opts.inJustDecodeBounds = false;
        Bitmap tempBitmap = BitmapFactory.decodeFile(file, opts);
        if (tempBitmap == null) {
            return null;
        }
        Matrix matrix = new Matrix();
        float scaleWidth = (float) reqWidth / (float) tempBitmap.getWidth();
        float scaleHeight = (float) reqHeight / (float) tempBitmap.getHeight();
        float scale = scaleHeight < scaleWidth ? scaleHeight : scaleWidth;
        matrix.postScale(scale, scale); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight(), matrix, true);
        if (!tempBitmap.equals(resizeBmp)) {
            tempBitmap.recycle();
        }
        return resizeBmp;
    }

    public static Bitmap getBitmapWithLowMemory(Bitmap tempBitmap, int reqWidth, int reqHeight) {
        Matrix matrix = new Matrix();
        float scaleWidth = (float) reqWidth / (float) tempBitmap.getWidth();
        float scaleHeight = (float) reqHeight / (float) tempBitmap.getHeight();
        float scale = scaleHeight < scaleWidth ? scaleHeight : scaleWidth;
        matrix.postScale(scale, scale); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight(), matrix, true);
        if (!tempBitmap.equals(resizeBmp)) {
            tempBitmap.recycle();
        }
        return resizeBmp;
    }

    public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
        Matrix m = new Matrix();
        m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        float targetX, targetY;
        if (orientationDegree == 90) {
            targetX = bm.getHeight();
            targetY = 0;
        } else {
            targetX = bm.getHeight();
            targetY = bm.getWidth();
        }
        final float[] values = new float[9];
        m.getValues(values);
        float x1 = values[Matrix.MTRANS_X];
        float y1 = values[Matrix.MTRANS_Y];
        m.postTranslate(targetX - x1, targetY - y1);
        Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        Canvas canvas = new Canvas(bm1);
        canvas.drawBitmap(bm, m, paint);
        return bm1;
    }

    public static Bitmap convert(Bitmap bitmapOrg, int Rotate) {
        Matrix m = new Matrix();
//        m.postScale(1, -1);   //镜像垂直翻转
//        m.postScale(-1, 1);   //镜像水平翻转
        m.postRotate(Rotate);  //旋转-90度
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                bitmapOrg.getWidth(), bitmapOrg.getHeight(), m, true);
        return resizedBitmap;
    }

}
